{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "from __future__ import (absolute_import, division, print_function, unicode_literals)\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.cm import get_cmap\n",
    "import cartopy.crs as crs\n",
    "import cartopy.feature as cfeature\n",
    "from netCDF4 import Dataset as nc\n",
    "\n",
    "from wrf import to_np, getvar, smooth2d\n",
    "\n",
    "# Open the output NetCDF with netcdf-python\n",
    "filename = \"/Users/ladwig/Documents/wrf_files/wrfout_d01_2016-10-07_00_00_00\"\n",
    "ncfile = nc(filename)\n",
    "\n",
    "# Get sea level pressure and cloud top temperature\n",
    "slp = getvar(ncfile, \"slp\")\n",
    "ctt = getvar(ncfile, \"ctt\")\n",
    "\n",
    "print (ctt)\n",
    "\n",
    "# Smooth the SLP\n",
    "smooth_slp = smooth2d(slp, 3)\n",
    "\n",
    "# Extract the latitude and longitude coordinate arrays as regular numpy array instead of xarray.DataArray\n",
    "lons = ctt.coords[\"XLONG\"]\n",
    "lats = ctt.coords[\"XLAT\"]\n",
    "\n",
    "# Get the cartopy projection class\n",
    "wrf_proj = slp.attrs[\"projection\"]\n",
    "cart_proj = wrf_proj.cartopy()\n",
    "\n",
    "# Create the figure\n",
    "fig = plt.figure(figsize=(8,8))\n",
    "ax = plt.axes([0.1,0.1,0.8,0.8], projection=cart_proj)\n",
    "\n",
    "# Download and create the states, land, and oceans using cartopy features\n",
    "states = cfeature.NaturalEarthFeature(category='cultural', scale='50m', facecolor='none',\n",
    "                             name='admin_1_states_provinces_shp')\n",
    "land = cfeature.NaturalEarthFeature(category='physical', name='land', scale='50m', \n",
    "                                    facecolor=cfeature.COLORS['land'])\n",
    "ocean = cfeature.NaturalEarthFeature(category='physical', name='ocean', scale='50m', \n",
    "                                     facecolor=cfeature.COLORS['water'])\n",
    "\n",
    "# Make the pressure contours.\n",
    "#contour_levels = [960, 962, 965, 967, 970, 975, 980, 985, 990, 995]\n",
    "#c1 = plt.contour(lons, lats, to_np(smooth_slp), levels=contour_levels, colors=\"white\", \n",
    "#            transform=crs.PlateCarree(), zorder=3, linewidth=1.3)\n",
    "\n",
    "# Add pressure contour labels\n",
    "#plt.clabel(c1, contour_levels, inline=True, fmt='%1.1f', fontsize=8)\n",
    "\n",
    "# Create the filled cloud top temperature contours\n",
    "#contour_levels = [-80, -70, -60, -50, -40, -30, -20, -10, 0, 10, 20, 30]\n",
    "contour_levels = np.arange(-80.0, 10.0, 10.0)\n",
    "plt.contourf(to_np(lons), to_np(lats), to_np(ctt), contour_levels, cmap=get_cmap(\"Greys\"),\n",
    "             transform=crs.PlateCarree(), zorder=2)\n",
    "\n",
    "# Create the label bar for cloud top temperature\n",
    "cb2 = plt.colorbar(ax=ax, shrink=.80)\n",
    "\n",
    "# Draw the oceans, land, and states\n",
    "ax.add_feature(ocean)\n",
    "ax.add_feature(land)\n",
    "ax.add_feature(states, linewidth=.5, edgecolor=\"black\")\n",
    "\n",
    "# Crop the domain to the region around the hurricane\n",
    "ax.set_extent([-85., -75.0, np.amin(lats), 30.0], crs=crs.PlateCarree())\n",
    "# Set the map limits\n",
    "#ax.set_xlim(wrf_proj.cartopy_xlim())\n",
    "#ax.set_ylim(wrf_proj.cartopy_ylim())\n",
    "\n",
    "ax.gridlines(crs=crs.PlateCarree(), draw_labels=False)\n",
    "\n",
    "# Add the title and show the image\n",
    "#plt.title(\"Hurricane Matthew Cloud Top Temperature (degC) and Sea Level Pressure (hPa)\")\n",
    "#plt.title(\"Hurricane Matthew Cloud Top Temperature (degC)\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
